package com.cman777.springc.mybatis.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cman777.springc.common.bean.Iface.EntityIFace;
import com.cman777.springc.common.helper.GenericUtil;

/**
 * @author chenzhicong
 * @time 2020/3/17 3:40
 * @description 对Service添加行为
 * TODO 20200317 CZC
 */
public class MyServiceContext<T extends EntityIFace> {
    private IService<T> baseService;
    public MyServiceContext(IService baseService) {
        this.baseService = baseService;
    }
    public static <T extends EntityIFace> MyServiceContext<T> ofService(IService<T> baseService) {
        return new MyServiceContext<T>(baseService);
    }

    /**
     * 获取实体类型，注意直接获取父类的实体泛型获取不到，
     * 因为这里可能注入的实际上是Cglib代理类，需要递归的去取父类
     */
    private Class getEntityClass(Class c) {
        if (c.getSuperclass() == Object.class) {
            return Object.class;
        } else {
            if (c.getSuperclass() == ServiceImpl.class) {
                return GenericUtil.getGenericClass(c,1);
            } else {
                return getEntityClass(c.getSuperclass());
            }
        }
    }



}
